
# pdf_cherry.py
# Author: Vasudev Ram - http://www.dancingbison.com
# Program to generate PDF using CherryPy and xtopdf.
# Requires Reportlab v1.21 open source version.
# Written using CherryPy 3.2.4 and Python 2.7.5.

import sys
import os
from PDFWriter import PDFWriter
from file_utils import change_file_ext
from textwrap import TextWrapper

import os
import cherrypy
from cherrypy.lib.static import serve_file

class Root(object):
    pass

class Upload(object):

    def index(self):
        return """
        <html><head><title>PDF in a CherryPy</title></head><body>
            <h3>PDF in a CherryPy</h3>
            <h4>Upload a text file to convert to PDF.</h4>
            <form action="text_to_pdf" method="post" enctype="multipart/form-data">
            filename: <input type="file" name="uploadFile" /><br/>
            <input type="submit"/>
            </form>
        </body></html>
        """
    index.exposed = True

    def text_to_pdf(self, uploadFile):
        try:
            txt_filename = uploadFile.filename
            pdf_filename = change_file_ext(txt_filename, ".txt", ".pdf")
            pw = PDFWriter(pdf_filename)
            pw.setFont("Courier", 10)
            pw.setHeader("Text file: " + txt_filename + \
                " PDF file: " + pdf_filename)
            pw.setFooter("Generated by CherryPy, xtopdf and Reportlab")
            wrapper = TextWrapper(width = 120, drop_whitespace = True,
                break_long_words = True)
            for lin in uploadFile.file.readlines():
                lines = wrapper.wrap(lin)
                for lin2 in lines:
                    pw.writeLine(lin)
            pw.close()
            respons = "<html><head><title>PDF in a CherryPy</title></head>"
            respons += "<body><h3>PDF in a CherryPy</h3>"
            respons += "Text file: " + txt_filename + "<br/>"
            respons += 'PDF file: <a href="/download/?filepath=' + os.getcwd() + \
                "\\" + pdf_filename + '">' + \
                pdf_filename + '</a><br/>'
            respons += "</body></html>"
            return respons
        except Exception, e:
            err_msg = "ERROR: In Upload.text_to_pdf(): Caught exception: " + \
                repr(e) + "\n"
            sys.stderr.write(err_msg)
            return err_msg
    text_to_pdf.exposed = True

class Download(object):

    def index(self, filepath):
        try:
            return serve_file(filepath, "application/x-download", "attachment")
        except Exception, e:
            err_msg = "ERROR: In Download.index(): Caught exception: " + \
                repr(e) + "\n"
            sys.stderr.write(err_msg)
            return err_msg
    index.exposed = True

def main():
    try:
        root = Root()
        root.upload = Upload()
        root.download = Download()
        cherrypy.quickstart(root)
    except Exception, e:
        err_msg = "ERROR: In main(): Caught exception: " + \
            repr(e) + "\n"
        sys.stderr.write(err_msg)
        sys.exit(1)

if __name__ == '__main__':
    main()

# EOF

